﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;
using log4net;
using ThreeByte.ActiveDeck.AddIn.Config;

namespace ThreeByte.ActiveDeck.Config
{
    public class DesignerSlideConfig : PageConfig
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DesignerSlideConfig));

        #region Public Properties

        private ObservableCollection<DesignerControl> _elements = new ObservableCollection<DesignerControl>();
        public ObservableCollection<DesignerControl> Elements {
            get {
                return _elements;
            }
            set {
                _elements = value;
                NotifyPropertyChanged("Elements");
            }
        }

        private ObservableCollection<LayerConfig> _layers = new ObservableCollection<LayerConfig>();
        public ObservableCollection<LayerConfig> Layers {
            get {
                return _layers;
            }
        }

        #endregion Public Properties


        public DesignerSlideConfig() {

        }

        public DesignerSlideConfig(XElement node, Dictionary<Guid, GraphicResource> resources)
            : base(node, resources) {

                foreach(XElement n in node.Elements(ns + "DesignerControl")) {
                    try {
                        DesignerControl newControl = DesignerControl.FromXml(n, resources);
                        Elements.Add(newControl);
                    } catch(Exception ex) {
                        log.Error("Could not create a designer control...continuing", ex);
                    }
                }

                foreach(XElement layerNode in node.Elements(LayerConfig.XML_NAME)) {
                    try {
                        Layers.Add(LayerConfig.FromXml(layerNode, resources));
                    } catch(Exception ex) {
                        log.Warn("Cannot reconstruct Layer", ex);
                    } 
                }
        }

        public override XElement ToXml() {
            XElement node = base.ToXml();

            foreach(DesignerControl d in Elements) {
                node.Add(d.ToXml());
            }

            foreach(LayerConfig layer in Layers) {
                node.Add(layer.ToXml());
            }
            return node;
        }

        public override List<GraphicResource> GetPersistentGraphicResources() {
            List<GraphicResource> imageList = new List<GraphicResource>();
            foreach(DesignerControl d in Elements) {
                foreach(GraphicResource g in d.GetGraphicResources()) {
                    imageList.Add(g);
                }
            }
            //foreach(LayerConfig layer in Layers) {
            //    foreach(DesignerControl d in layer.Elements) {
            //        foreach(GraphicResource g in d.GetGraphicResources()) {
            //            imageList.Add(g);
            //        }
            //    }
            //}
            return imageList;
        }
 
    }
}
